package com.cjxy.parking.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjxy.parking.entity.po.Notice;
import com.cjxy.parking.entity.query.NoticeQuery;
import com.cjxy.parking.mapper.NoticeMapper;
import com.cjxy.parking.service.NoticeService;
import com.cjxy.parking.utils.UserContext;
import lombok.val;
import org.aspectj.weaver.ast.Not;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @auther huangsenji
 * @date 2025/10/26
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    
    @Override
    public IPage<Notice> pageNotice(NoticeQuery noticeQuery) {
        // 创建分页对象
        IPage<Notice> page = new Page<>(noticeQuery.getPagenum(), noticeQuery.getPageSize());
        
        // 构建查询条件
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getStatus, 1); // 只查询未删除的公告
        
        // 根据标题模糊查询
        if (noticeQuery.getTitle() != null && !noticeQuery.getTitle().isEmpty()) {
            queryWrapper.like(Notice::getTitle, noticeQuery.getTitle());
        }
        
        // 根据内容模糊查询
        if (noticeQuery.getContent() != null && !noticeQuery.getContent().isEmpty()) {
            queryWrapper.like(Notice::getContent, noticeQuery.getContent());
        }
        
        // 按创建时间倒序排列
        queryWrapper.orderByDesc(Notice::getCreateTime);
        
        return this.page(page, queryWrapper);
    }

    /**
     * 添加公告
     *
     * @param notice
     * @return
     */
    @Override
    public boolean saveNotice(Notice notice) {
        if (Objects.isNull(notice) || StrUtil.isBlank(notice.getTitle()) || StrUtil.isBlank(notice.getContent())) {
            throw new RuntimeException("参数错误，请重新输入");
        }
        // 根据标题名查询
        Notice noticeByTitle = getNoticeByTitle(notice.getTitle());
        if (!Objects.isNull(noticeByTitle)) {
            throw new RuntimeException("标题已存在");
        }
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setStatus(1);
        notice.setOperator(UserContext.getUsername());
        return save(notice);
    }

    /**
     * 更新公告
     *
     * @param notice
     * @return
     */
    @Override
    public boolean updateNotice(Notice notice) {
        if (Objects.isNull(notice) || StrUtil.isBlank(notice.getTitle()) || StrUtil.isBlank(notice.getContent())) {
            throw new RuntimeException("参数错误，请重新输入");
        }
        Notice noticeById = getById(notice.getId());
        if (Objects.isNull(noticeById)) {
            throw new RuntimeException("公告不存在");
        }
        // 是否标题存在
        if (!StrUtil.equals(notice.getTitle(), noticeById.getTitle())) {
            Notice noticeByTitle = getNoticeByTitle(notice.getTitle());
            if (!Objects.isNull(noticeByTitle)) {
                throw new RuntimeException("标题已存在");
            }
        }
        // 同时修改创建时间。设为最新
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setOperator(UserContext.getUsername());
        return updateById(notice);
    }


    /**
     *  根据标题名查询
     */
    private Notice getNoticeByTitle(String title) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getTitle, title);
        queryWrapper.eq(Notice::getStatus, 1);
        return getOne(queryWrapper);
    }

    /**
     * 获取最新公告
     *
     * @return
     */
    @Override
    public Notice getLastNotice() {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getStatus, 1);
        queryWrapper.orderByDesc(Notice::getCreateTime);
        queryWrapper.last("limit 1");
        return getOne(queryWrapper);
    }

    /**
     * 获取最新的三个公告
     *
     * @return
     */
    @Override
    public List<Notice> getLastThree() {

        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getStatus, 1);
        queryWrapper.orderByDesc(Notice::getCreateTime);
        queryWrapper.last("limit 3");
        return list(queryWrapper);
    }
}